package org.xiaoleng.cdm.convertors;

import com.alibaba.fastjson2.JSON;
import org.xiaoleng.cdm.enums.DatasourceType;
import org.xiaoleng.cdm.datasources.Datasource;
import org.xiaoleng.cdm.datasources.MysqlDatasource;
import org.xiaoleng.cdm.datasources.RedisDatasource;
import org.xiaoleng.cdm.entity.ro.JobConfigurationRO;
import org.xiaoleng.cdm.entity.dto.JobConfiguration;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class JobConfigConvertor {

    private static Map<DatasourceType, Function<Object, Datasource>> mapping = new HashMap<>();

    static {
        mapping.put(DatasourceType.MYSQL, o -> JSON.parseObject(JSON.toJSONString(o), MysqlDatasource.class));
        mapping.put(DatasourceType.REDIS, o -> JSON.parseObject(JSON.toJSONString(o), RedisDatasource.class));
    }

    public static JobConfiguration convert(JobConfigurationRO jobConfigurationRO) {
        return new JobConfiguration(jobConfigurationRO.getJobName(),
                mapping.get(jobConfigurationRO.getSourceType()).apply(jobConfigurationRO.getSource()),
                mapping.get(jobConfigurationRO.getTargetType()).apply(jobConfigurationRO.getTarget()));
    }
}
